package com.seemsea.utils.qrcode;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;

import com.seemsea.common.Global;
import com.swetake.util.Qrcode;

public class QrcodeEncoder {
	 /** 
     * ������ά������ 
     * @param param �������֤���� 
     * @throws Exception 
     */  
    public static void qrcodeEncoder(String param,String filePath) throws Exception {  
        Qrcode qrcode = new Qrcode();  
        qrcode.setQrcodeErrorCorrect('M');  
        qrcode.setQrcodeEncodeMode('B');  
        qrcode.setQrcodeVersion(7);  
  
        byte[] bstr = param.getBytes("UTF-8");  
        BufferedImage bi = new BufferedImage(139, 139,BufferedImage.TYPE_INT_RGB);  
        Graphics2D g = bi.createGraphics();  
        g.setBackground(Color.WHITE);   //������ɫ  
        g.clearRect(0, 0, 139, 139);  
        g.setColor(Color.BLACK);    //������ɫ  
        if (bstr.length > 0 && bstr.length < 123) {  
            boolean[][] b = qrcode.calQrcode(bstr);  
            for (int i = 0; i < b.length; i++) {  
                for (int j = 0; j < b.length; j++) {  
                    if (b[j][i]) {  
                        g.fillRect(j * 3 + 2, i * 3 + 2, 3, 3);  
                    }  
                }  
  
            }  
        }  
        g.dispose();  
        bi.flush();

        File f = new File(filePath); 
        ImageIO.write(bi, Global.QRCODEIMGTYPE, f);  
    } 
}
